package com.onehouse.trigger.listener;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.onehouse.domain.checkIn.service.ICheckInService;
import com.onehouse.domain.payment.adapter.event.CheckInOrderPaymentTerminateEvent;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CheckInPayFailListener {

    @Autowired
    ICheckInService checkInService;

    @Autowired
    private EventBus eventBus;

    @PostConstruct
    public void init() {
        eventBus.register(this);
    }

    @Subscribe
    public void handleEvent(CheckInOrderPaymentTerminateEvent checkInPaySuccessMessageEvent) {
        try {
            checkInService.processFailPayment(new RuntimeException(checkInPaySuccessMessageEvent.getTerminateReason()));
        } catch (Exception e) {
            checkInService.processFailPayment(e);
        }
    }
}
